package com.ciwong.epaper.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.MenuOptionBean;
import com.ciwong.mobilelib.widget.CWDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/7.
 */

public class BaseBottomMenuDialog extends CWDialog {

    private Context mContext;
    private View mRootView;

    private ListView list_view;

    private TextView tx_dialog_title;
    private LinearLayout mLayoutFirst, mLayoutSecond;

    private List<String> menuOptionBeanList = new ArrayList<>();

    private BottomMenuAdapter mBottomMenuAdapter;

    public BaseBottomMenuDialog(Context context) {
        super(context);
        mContext = context;
        init();
    }


    private void init() {

        mRootView = LayoutInflater.from(this.getContext()).inflate(R.layout.base_bottom_menu_dialog, null);
        list_view = (ListView) mRootView.findViewById(R.id.list_view);
        tx_dialog_title = (TextView) mRootView.findViewById(R.id.tx_dialog_title);
        mLayoutFirst = (LinearLayout) mRootView.findViewById(R.id.ll_first);
        mLayoutSecond = (LinearLayout) mRootView.findViewById(R.id.ll_second);

        mRootView.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mBottomMenuAdapter = new BottomMenuAdapter();
        list_view.setAdapter(mBottomMenuAdapter);
        setContentView(mRootView, true);
        setCanceledOnTouchOutside(true);

    }

    public void setOtherStyle(boolean b) {
        if (b) {
            mLayoutFirst.setVisibility(View.VISIBLE);
            mLayoutSecond.setVisibility(View.GONE);
        }

    }

    public void setTitle(String titleText) {
        tx_dialog_title.setText(titleText);
    }

    public void setItemList(List<String> itemList) {
        menuOptionBeanList.clear();
        menuOptionBeanList.addAll(itemList);
        mBottomMenuAdapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(@Nullable AdapterView.OnItemClickListener listener) {
        list_view.setOnItemClickListener(listener);
    }

    public int getOptionCount() {
        return menuOptionBeanList.size();
    }

    /**
     * 显示菜单
     */
    public void show() {
        super.showFromBottom();
    }


    public class BottomMenuAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return menuOptionBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return menuOptionBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_base_bottom_menu_option, null);
            }
            TextView optionText = convertView.findViewById(R.id.tx_option_text);
            if (position < menuOptionBeanList.size()) {
                optionText.setText(menuOptionBeanList.get(position));
            }
            return convertView;
        }


    }

}
